<template>
  <div>
    <el-card>
      <!-- 面包屑 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索表单 -->
      <el-form
        :inline="true"
        :model="swiperForm"
        class="demo-form-inline"
        size="large"
        style="margin-top: 25px"
      >
        <el-form-item label="跳转链接">
          <el-input v-model="swiperForm.link" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="图片alt">
          <el-input v-model="swiperForm.alt" placeholder="请输入图片alt" />
        </el-form-item>
        <el-form-item>
          <el-button type="default" @click="clear">清空</el-button>
          <el-button type="primary" @click="getSwiperListData">搜索</el-button>
          <el-button type="success" @click="addDialog">新增轮播图</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 12px">
      <el-table :data="swiperList" style="width: 100%">
        <el-table-column label="轮播图图片" prop="img">
          <template #default="{ row }">
            <el-image
              style="width: 100px; height: 80px"
              :src="row.img"
              :preview-src-list="[row.img]"
              preview-teleported
            >
              <template #error>
                <div class="image-slot">
                  <img v-error />
                </div>
              </template>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="跳转地址" prop="link">
          <template #default="{ row }">
            <a
              :href="row.link"
              target="_blank"
              style="text-decoration: none; color: #606266"
              >{{ row.link }}</a
            >
          </template>
        </el-table-column>
        <el-table-column label="图片alt" prop="alt" />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-switch :model-value="row.flag"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" @click="revise(row)">修改</el-button>
            <el-button type="danger" @click="del(row.bannerid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model::current-page="swiperForm.page"
        :page-sizes="[1, 3, 5, 10]"
        v-model::page-size="swiperForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <Child ref="dialogRef" />
  </div>
</template>

<script>
import {
  getSwiperList,
  AddSwiper,
  updateSwiper,
  deleteSwiper
} from '@/api/swiper'
import Child from '@/components/Dialog'
export default {
  data () {
    return {
      swiperList: [],
      total: 0,
      swiperForm: {
        page: 1,
        pageSize: 3,
        link: '',
        alt: ''
      }
    }
  },
  components: { Child },
  created () {
    this.getSwiperListData()
  },
  methods: {
    async getSwiperListData () {
      const res = await getSwiperList(this.swiperForm)
      this.swiperList = res.data.items
      this.total = res.data.total
      // console.log(res)
    },
    addDialog () {
      this.$refs.dialogRef.dialogVisible = true
    },
    clear () {
      this.swiperForm = {
        page: 1,
        pageSize: 3,
        link: '',
        alt: ''
      }
    },
    //每页条数改变
    handleSizeChange (pageSize) {
      this.swiperForm.pageSize = pageSize
      this.getSwiperListData()
    },
    //当前页发生改变
    handleCurrentChange (page) {
      this.swiperForm.page = page
      this.getSwiperListData()
    },
    //修改轮播图
    revise (val) {
      this.$refs.dialogRef.dialogVisible = true
      this.$refs.dialogRef.mode = 'edit'
      //z展开是一层深拷贝
      this.$refs.dialogRef.dialogForm = { ...val }
      //完全深拷贝
      // console.log(val)
      this.$refs.dialogRef.fileList.push({
        name: val.alt,
        url: val.img
      })
    },
    //删除轮播图
    async del (val) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          await deleteSwiper(val)
          this.swiperForm.page = 1
          this.getSwiperListData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.image-slot {
  width: 80px;
  height: 80px;
  display: flex;
  justify-content: center;
  img {
    width: 90%;
    height: 90%;
    margin-top: 5px;
  }
}
</style>
